Skip to content

build: add ts support in core modules#62146

Open
marco-ippolito wants to merge 5 commits intonodejs:mainfrom
marco-ippolito:native-ts
Open

build: add ts support in core modules#62146
marco-ippolito wants to merge 5 commits intonodejs:mainfrom
marco-ippolito:native-ts

Conversation

@marco-ippolito
Copy link
Member

@marco-ippolito marco-ippolito commented Mar 7, 2026

Let's try again.
This PR allows Node.js source code to be written in TS.
This is semver major because the build now always requires rust to be installed.
This adds swc as a rust crate dependency and it's only use during build time.
Technically we could replace amaro wasm with this for type stripping but it's not the goal of this PR and in needs other considerations.
I moved an internal from .js and .ts to showcase
Also added a flag so that the transpiled code can be writte on the fs for debugging.
A lot of the addition are vendored crates sorry for the massive PR
I used AI to help me out with things I dont know (rust) so please review carefully
I had to bump the rustc from 1.82 to 1.85, obviously this cannot land if we dont update that on the CI machines nodejs/build#4245

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/build
  • @nodejs/gyp
  • @nodejs/security-wg
  • @nodejs/tsc

@nodejs-github-bot nodejs-github-bot added build Issues and PRs related to build files or the CI. dependencies Pull requests that update a dependency file. doc Issues and PRs related to the documentations. needs-ci PRs that need a full CI run. labels Mar 7, 2026
@marco-ippolito marco-ippolito added semver-major PRs that contain breaking changes and should be released in the next major version. strip-types Issues or PRs related to strip-types support labels Mar 7, 2026
@marco-ippolito marco-ippolito changed the title build: add type ts support in core build: add ts support in core Mar 7, 2026
@marco-ippolito marco-ippolito changed the title build: add ts support in core build: add ts support in core modules Mar 7, 2026
@marco-ippolito marco-ippolito marked this pull request as ready for review March 7, 2026 15:28
@marco-ippolito marco-ippolito added the blocked PRs that are blocked by other issues or PRs. label Mar 7, 2026
@anonrig
Copy link
Member

anonrig commented Mar 7, 2026

@marco-ippolito do you know what steps are needed to run and expose Rust code just like C++?

@marco-ippolito
Copy link
Member Author

marco-ippolito commented Mar 7, 2026

@marco-ippolito do you know what steps are needed to run and expose Rust code just like C++?

its already done in this PR, see d9ee95f

basically add the crate in the deps/crates cargo.toml, create a header file if doesnt come with the crate and add it to the node gyp

@ljharb
Copy link
Member

ljharb commented Mar 7, 2026

Will this type strip at runtime? Or will it do it at build time?

@marco-ippolito
Copy link
Member Author

marco-ippolito commented Mar 7, 2026

Will this type strip at runtime? Or will it do it at build time?

Build time

@ljharb
Copy link
Member

ljharb commented Mar 7, 2026

How does this interact with the --node-builtin-modules-path flag? will it still type strip at runtime in that case?

@marco-ippolito
Copy link
Member Author

How does this interact with the --node-builtin-modules-path flag? will it still type strip at runtime in that case?

Yes, I tested it, it works fine. it plugs in the js2c module

@mcollina
Copy link
Member

mcollina commented Mar 7, 2026

I'm relatively concerned about adding 1 million and 300 hundred thousand lines of code, even if they are just dependencies.

Is there a solution that does not require this much?

@marco-ippolito
Copy link
Member Author

marco-ippolito commented Mar 7, 2026

I dont think so since we are vendoring dependencies. I think the only solution is to have an automation generate them but its kinda impossible to review anyways. (The temporal PR from @legendecas had the same problem)

@legendecas
Copy link
Member

legendecas commented Mar 7, 2026

I think the question should be is it necessary to have every swc dependencies to be included?

I listed the newly added dependencies, and the followings are the biggest deps that I doubt if they are required:

9.6M	deps/crates/vendor/tracing          => Diagnostic purpose
5.1M	deps/crates/vendor/libc             => Indirect dep of crates `cpufeatures` and `num_cpus`
2.8M	deps/crates/vendor/regex-automata   => Regex engine
1.6M	deps/crates/vendor/bitvec           => Indirect dep of swc_sourcemap
1.6M	deps/crates/vendor/zerocopy         => Indirect dep of hashbrown
1.3M	deps/crates/vendor/zerocopy-derive  => Indirect dep of hashbrown
1.0M	deps/crates/vendor/idna             => Indirect dep of crate `url`

Additionally, crates like wasm-bindgen are not technically needed for the PR purpose.

I think technically many of these deps can be stripped down.

@marco-ippolito
Copy link
Member Author

Can I just like delete them and see if it builds?

@marco-ippolito
Copy link
Member Author

marco-ippolito commented Mar 8, 2026

I tried to remove those crates but it seems they are all required to build. Idk if there is a way to know exactly which ones are unused or how to remove them, I'm no rust expert

@targos
Copy link
Member

targos commented Mar 8, 2026

This is blocked by nodejs/build#4245

@marco-ippolito
Copy link
Member Author

Node.js configure: Found Python 3.12.3...
INFO: configure completed successfully
SKIP_XZ=1 supplied, skipping .tar.xz creation
mkdir -p out/doc
cp doc/node-config-schema.json out/doc
mkdir -p out/doc/api
cp -r doc/api out/doc

added 323 packages, and audited 325 packages in 6s

136 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
[09:05:04.899] ERROR: Unknown file extension ".wasm" for /home/runner/work/node/node/tools/doc/node_modules/@minify-html/wasm/index_bg.wasm
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".wasm" for /home/runner/work/node/node/tools/doc/node_modules/@minify-html/wasm/index_bg.wasm
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:189:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:232:36)
    at defaultLoad (node:internal/modules/esm/load:145:22)
make: *** [Makefile:845: out/doc/api/addons.html] Error 1
    at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:543:45)

this seems unrelated but consistent 🫤

@marco-ippolito marco-ippolito force-pushed the native-ts branch 3 times, most recently from 6fa7a86 to 7f79144 Compare March 8, 2026 09:20
@ShogunPanda
Copy link
Contributor

I'm relatively concerned about adding 1 million and 300 hundred thousand lines of code, even if they are just dependencies.

Is there a solution that does not require this much?

I had she same concern in another PR I was working on. (no spoiler ;P)
I wonder if it would make sense to track such deps in another repo or similar solutions.

@codecov
Copy link

codecov bot commented Mar 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.65%. Comparing base (3725bd2) to head (5c971a2).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62146      +/-   ##
==========================================
- Coverage   89.65%   89.65%   -0.01%     
==========================================
  Files         676      675       -1     
  Lines      206543   206492      -51     
  Branches    39547    39532      -15     
==========================================
- Hits       185184   185133      -51     
+ Misses      13480    13474       -6     
- Partials     7879     7885       +6     

see 38 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked PRs that are blocked by other issues or PRs. build Issues and PRs related to build files or the CI. dependencies Pull requests that update a dependency file. doc Issues and PRs related to the documentations. needs-ci PRs that need a full CI run. semver-major PRs that contain breaking changes and should be released in the next major version. strip-types Issues or PRs related to strip-types support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants